<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        #video {
            /* border: 1px solid #000; */
        }
        #cvs {
            /* border: 1px solid #000; */
        }
        #cvs2 {
            /* border: 1px solid #000; */
        }
    </style>
</head>
<body>
    <video id="video" src="video.mp4" loop oncanplay="init()" controls width="200"></video>
    <canvas id="cvs" onclick="video.play()"></canvas>
    <canvas id="cvs2" onclick="video.play()"></canvas>


    <script>
        const init = () => {
            const video = document.querySelector('#video')
            const cvs = document.querySelector('#cvs')
            const cvs2 = document.querySelector('#cvs2')

            cvs.width = cvs2.width = video.offsetWidth
            cvs.height = cvs2.height = video.offsetHeight

            const ctx = cvs.getContext('2d')
            const ctx2 = cvs2.getContext('2d')

            const playVideo = () => {
                requestAnimationFrame(playVideo)
                const { width, height } = cvs
                ctx.drawImage(video, 0, 0, width, height)
                
                const data =  ctx.getImageData(0, 0, width, height).data
                // ctx2.clearRect(0, 0, width, height)
                const bl = 8
                ctx2.font = '12px serif'
                for (let i = 0; i < data.length; i += 4 * bl) {
                    const x = parseInt((i + 1) % (width * 4) / 4)
                    const y = parseInt(i / (width * 4))
                    if (x % bl === 0 && y % bl === 0) {
                        const g = parseInt((data[i] + data[i + 1] + data[i + 2]) / 1.5)
                        ctx2.fillStyle = `rgba(${g}, ${g}, ${g}, ${data[i + 3]})`
                        ctx2.fillText('8', x, y)
                    }
                }
            }
            playVideo()
        }
    </script>
</body>
</html>